'Create the shell object
Set shell = CreateObject("WScript.Shell")

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' THE CUSTOM ACTION DATA "CustomActionData"
' Session.Property("CustomActionData") = "[TARGETDIR]|[WORDINSTALLED]|[EXCELINSTALLED]|[POWERPOINTINSTALLED]|[WORDADDINCHECKBOX]|[EXCELADDINCHECKBOX]|[POWERPOINTADDINCHECKBOX]" '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'Get the custom data from the MSI for manipulating custom action
CustomData = Session.Property("CustomActionData")
Delimiter = "|"
CustomDataArgs = Split( CustomData, Delimiter )

'Create data from "CustomActionData"
TARGETDIR = CustomDataArgs(0)
WordInstalled = CustomDataArgs(1)
ExcelInstalled = CustomDataArgs(2)
PowerPointInstalled = CustomDataArgs(3)
WordCheckBox = CustomDataArgs(4)
ExcelCheckBox = CustomDataArgs(5)
PowerPointCheckBox = CustomDataArgs(6)

'Setting the Manifest's value to write as ".vsto" instead of visual studio's default ".manifest"
If WordCheckBox = "1" Then
	shell.RegWrite "HKCU\Software\Microsoft\Office\Word\Addins\Microsoft.ExternalResearch.CreativeCommons.Word\Manifest", TARGETDIR & "Microsoft.ExternalResearch.CreativeCommons.Word.vsto", "REG_SZ"
else
    if KeyExists("HKCU\Software\Microsoft\Office\Word\Addins\Microsoft.ExternalResearch.CreativeCommons.Word\Manifest") = True Then' Checking if repair option called
        'Get the application installation folder
        InstallPath = shell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Addins\Microsoft.ExternalResearch.CreativeCommons\InstallRoot")
        shell.RegWrite "HKCU\Software\Microsoft\Office\Word\Addins\Microsoft.ExternalResearch.CreativeCommons.Word\Manifest", InstallPath & "Microsoft.ExternalResearch.CreativeCommons.Word.vsto", "REG_SZ"
        shell.RegWrite "HKCU\Software\Microsoft\Office\Word\Addins\Microsoft.ExternalResearch.CreativeCommons.Word\LoadBehavior",3 , "REG_DWORD"
    end if
End If
If ExcelCheckBox = "1" Then
	shell.RegWrite "HKCU\Software\Microsoft\Office\Excel\Addins\Microsoft.ExternalResearch.CreativeCommons.Excel\Manifest", TARGETDIR & "Microsoft.ExternalResearch.CreativeCommons.Excel.vsto", "REG_SZ"
else
    if KeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\Microsoft.ExternalResearch.CreativeCommons.Excel\Manifest") = True Then' Checking if repair option called
        'Get the application installation folder
        InstallPath = shell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Addins\Microsoft.ExternalResearch.CreativeCommons\InstallRoot")
        shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\Microsoft.ExternalResearch.CreativeCommons.Excel\Manifest", InstallPath & "Microsoft.ExternalResearch.CreativeCommons.Excel.vsto", "REG_SZ"
        shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\Addins\Microsoft.ExternalResearch.CreativeCommons.Excel\LoadBehavior",3 , "REG_DWORD"
    end if
End If
If PowerPointCheckBox = "1" Then
	shell.RegWrite "HKCU\Software\Microsoft\Office\PowerPoint\Addins\Microsoft.ExternalResearch.CreativeCommons.PowerPoint\Manifest", TARGETDIR & "Microsoft.ExternalResearch.CreativeCommons.PowerPoint.vsto", "REG_SZ"
Else
    if KeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\PowerPoint\Addins\Microsoft.ExternalResearch.CreativeCommons.PowerPoint\Manifest") = True then ' Checking if repair option called
        'Get the application installation folder
        InstallPath = shell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Addins\Microsoft.ExternalResearch.CreativeCommons\InstallRoot")
        shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\PowerPoint\Addins\Microsoft.ExternalResearch.CreativeCommons.PowerPoint\Manifest", InstallPath & "Microsoft.ExternalResearch.CreativeCommons.PowerPoint.vsto", "REG_SZ"
        shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Office\PowerPoint\Addins\Microsoft.ExternalResearch.CreativeCommons.PowerPoint\LoadBehavior",3 , "REG_DWORD"
    end if
End If
Function KeyExists (key)
Dim key2
On Error Resume Next
Set shell = CreateObject("WScript.Shell")
key2 = Shell.RegRead (key)
If err <> 0 Then
KeyExists = False
Else
KeyExists = True
End if
End Function
